﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;
using HYRevitCode.CurveUtility;
using HYRevitCode.XYZUtility;
using Teigha.Core;
using Teigha.TD;

namespace QuickModel.InteractiveOperation
{
	public class DwgEntitiesSelection
	{
		public DwgEntitiesSelectionResult Result
		{
			get
			{
				return this.m_result;
			}
		}

		public DwgEntitiesSelection(UIDocument uiDocument)
		{
			this.m_uiDocument = uiDocument;
			this.m_doc = uiDocument.Document;
		}

		public virtual bool TryPickSameLayerCurve(out List<CurveInfo> lstCurve, bool bHideLayer = false, SelecteEntType type = SelecteEntType.eLine, string strPickGuidInfo = "")
		{
			lstCurve = new List<CurveInfo>();
			this.m_result = DwgEntitiesSelectionResult.eOK;
			try
			{
				DwgEntitiesSelectFilter dwgEntitiesSelectFilter = new DwgEntitiesSelectFilter(this.m_doc, type, null);
				Reference reference = this.m_uiDocument.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)2, dwgEntitiesSelectFilter, strPickGuidInfo);
				Element element = this.m_doc.GetElement(reference);
				GeometryElement geometryElement = element.get_Geometry(new Options());
				GeometryObject geometryObjectFromReference = element.GetGeometryObjectFromReference(reference);
				bool flag = !this.PreCheck(element);
				if (flag)
				{
					return false;
				}
				string layerName = string.Empty;
				Category category = null;
				ElementId elementId = null;
				bool flag2 = !this.GetGraphicsInfoFromGeometryObject(out elementId, out category, geometryObjectFromReference, bHideLayer);
				if (flag2)
				{
					this.m_result = DwgEntitiesSelectionResult.eDataError;
					return false;
				}
				bool flag3 = category == null;
				if (flag3)
				{
					this.m_result = DwgEntitiesSelectionResult.eDataError;
					return false;
				}
				layerName = category.Name;
				foreach (GeometryObject geometryObject in geometryElement)
				{
					GeometryInstance geometryInstance = geometryObject as GeometryInstance;
					Transform transform = geometryInstance.Transform;
					bool flag4 = null != geometryInstance;
					if (flag4)
					{
						foreach (GeometryObject geometryObject2 in geometryInstance.SymbolGeometry)
						{
							ElementId elementId2 = null;
							Category category2 = null;
							bool flag5 = !this.GetGraphicsInfoFromGeometryObject(out elementId2, out category2, geometryObject2, false);
							if (!flag5)
							{
								bool flag6 = category2.Id != category.Id;
								if (!flag6)
								{
									bool flag7 = !DwgEntitiesSelectFilter.IsSpecifiedType(geometryObject2, type);
									if (!flag7)
									{
										bool flag8 = geometryObject2 is Curve;
										if (flag8)
										{
											Curve curve = geometryObject2 as Curve;
											CurveInfo curveInfo = new CurveInfo(curve.CreateTransformed(transform), layerName);
											curveInfo.RevitLayer = category;
											lstCurve.Add(curveInfo);
										}
										else
										{
											bool flag9 = geometryObject2 is PolyLine;
											if (flag9)
											{
												PolyLine polyLine = geometryObject2 as PolyLine;
												IList<XYZ> coordinates = polyLine.GetCoordinates();
												XYZ xyz = coordinates[0];
												for (int i = 0; i < coordinates.Count - 1; i++)
												{
													XYZ xyz2 = coordinates[i + 1];
													bool flag10 = xyz.DistanceTo(xyz2) < this.m_doc.Application.ShortCurveTolerance;
													if (!flag10)
													{
														Line line = Line.CreateBound(xyz, xyz2);
														xyz = coordinates[i + 1];
														CurveInfo curveInfo2 = new CurveInfo(line.CreateTransformed(transform), layerName);
														curveInfo2.RevitLayer = category;
														lstCurve.Add(curveInfo2);
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				this.m_result = DwgEntitiesSelectionResult.eCancel;
				throw;
			}
			catch (Exception ex)
			{
				Trace.Assert(true, ex.Message);
				this.m_result = DwgEntitiesSelectionResult.eOtherError;
				return false;
			}
			return true;
		}

		public virtual bool TryPickSameLayerAllCurve(out List<CurveInfo> lstCurve, bool bHideLayer = false, SelecteEntType type = SelecteEntType.eLine, string strPickGuidInfo = "")
		{
			lstCurve = new List<CurveInfo>();
			this.m_result = DwgEntitiesSelectionResult.eOK;
			try
			{
				DwgEntitiesSelectFilter dwgEntitiesSelectFilter = new DwgEntitiesSelectFilter(this.m_doc, type, null);
				Reference reference = this.m_uiDocument.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)2, dwgEntitiesSelectFilter, strPickGuidInfo);
				Element element = this.m_doc.GetElement(reference);
				GeometryElement geometryElement = element.get_Geometry(new Options());
				GeometryObject geometryObjectFromReference = element.GetGeometryObjectFromReference(reference);
				bool flag = !this.PreCheck(element);
				if (flag)
				{
					return false;
				}
				string layerName = string.Empty;
				Category category = null;
				ElementId elementId = null;
				bool flag2 = !this.GetGraphicsInfoFromGeometryObject(out elementId, out category, geometryObjectFromReference, bHideLayer);
				if (flag2)
				{
					this.m_result = DwgEntitiesSelectionResult.eDataError;
					return false;
				}
				bool flag3 = category == null;
				if (flag3)
				{
					this.m_result = DwgEntitiesSelectionResult.eDataError;
					return false;
				}
				layerName = category.Name;
				foreach (GeometryObject geometryObject in geometryElement)
				{
					GeometryInstance geometryInstance = geometryObject as GeometryInstance;
					bool flag4 = null != geometryInstance;
					if (flag4)
					{
						foreach (GeometryObject geometryObject2 in geometryInstance.GetInstanceGeometry())
						{
							ElementId elementId2 = null;
							Category category2 = null;
							bool flag5 = !this.GetGraphicsInfoFromGeometryObject(out elementId2, out category2, geometryObject2, false);
							if (!flag5)
							{
								bool flag6 = category2.Id != category.Id;
								if (!flag6)
								{
									bool flag7 = !DwgEntitiesSelectFilter.IsSpecifiedType(geometryObject2, type);
									if (!flag7)
									{
										bool flag8 = geometryObject2 is Curve;
										if (flag8)
										{
											Curve curve = geometryObject2 as Curve;
											CurveInfo curveInfo = new CurveInfo(curve, layerName);
											curveInfo.RevitLayer = category;
											lstCurve.Add(curveInfo);
										}
										else
										{
											bool flag9 = geometryObject2 is PolyLine;
											if (flag9)
											{
												PolyLine polyLine = geometryObject2 as PolyLine;
												IList<XYZ> coordinates = polyLine.GetCoordinates();
												XYZ xyz = coordinates[0];
												for (int i = 0; i < coordinates.Count - 1; i++)
												{
													XYZ xyz2 = coordinates[i + 1];
													bool flag10 = xyz.DistanceTo(xyz2) < this.m_doc.Application.ShortCurveTolerance;
													if (!flag10)
													{
														Line curve2 = Line.CreateBound(xyz, xyz2);
														xyz = coordinates[i + 1];
														CurveInfo curveInfo2 = new CurveInfo(curve2, layerName);
														curveInfo2.RevitLayer = category;
														lstCurve.Add(curveInfo2);
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				this.m_result = DwgEntitiesSelectionResult.eCancel;
				throw;
			}
			catch (Exception ex)
			{
				Trace.Assert(true, ex.Message);
				this.m_result = DwgEntitiesSelectionResult.eOtherError;
				return false;
			}
			return true;
		}

		public virtual bool TryPickOneCurveLimitLayer(out CurveInfo curveInfo, List<ElementId> lstLayer, SelecteEntType type = SelecteEntType.eLine, string strPickGuidInfo = "")
		{
			curveInfo = null;
			this.m_result = DwgEntitiesSelectionResult.eOK;
			try
			{
				DwgEntitiesSelectFilter dwgEntitiesSelectFilter = new DwgEntitiesSelectFilter(this.m_doc, type, lstLayer);
				Reference reference = this.m_uiDocument.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)2, dwgEntitiesSelectFilter, strPickGuidInfo);
				Element element = this.m_doc.GetElement(reference);
				GeometryElement geometryElement = element.get_Geometry(new Options());
				GeometryObject geometryObjectFromReference = element.GetGeometryObjectFromReference(reference);
				bool flag = !this.PreCheck(element);
				if (flag)
				{
					return false;
				}
				string layerName = string.Empty;
				Category category = null;
				ElementId elementId = null;
				bool flag2 = !this.GetGraphicsInfoFromGeometryObject(out elementId, out category, geometryObjectFromReference, false);
				if (flag2)
				{
					this.m_result = DwgEntitiesSelectionResult.eDataError;
					return false;
				}
				bool flag3 = category == null;
				if (flag3)
				{
					this.m_result = DwgEntitiesSelectionResult.eDataError;
					return false;
				}
				layerName = category.Name;
				bool flag4 = geometryObjectFromReference is Curve;
				if (flag4)
				{
					Curve curve = geometryObjectFromReference as Curve;
					curveInfo = new CurveInfo(curve, layerName);
					curveInfo.RevitLayer = category;
				}
				else
				{
					bool flag5 = geometryObjectFromReference is PolyLine;
					if (flag5)
					{
						PolyLine polyLine = geometryObjectFromReference as PolyLine;
						IList<XYZ> coordinates = polyLine.GetCoordinates();
						for (int i = 0; i < coordinates.Count - 1; i++)
						{
							Line line = Line.CreateBound(coordinates[i].SetZ(0.0), coordinates[i + 1].SetZ(0.0));
							bool flag6 = CurveUtilityMethod.IfPointOnCurve(line, reference.GlobalPoint.SetZ(0.0), true);
							if (flag6)
							{
								curveInfo = new CurveInfo(line, layerName);
								curveInfo.RevitLayer = category;
								break;
							}
						}
					}
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				this.m_result = DwgEntitiesSelectionResult.eCancel;
				throw;
			}
			catch (Exception ex)
			{
				Trace.Assert(true, ex.Message);
				this.m_result = DwgEntitiesSelectionResult.eOtherError;
				return false;
			}
			return true;
		}

		public virtual bool TryPickSameNameBlockRef<T>(out List<T> lstBlockInfo, bool bPointBox = false, string strPickGuidInfo = "", string strPickBoxGuidInfo = "", bool bOnlySameSize = false) where T : BlockInfo
		{
			lstBlockInfo = new List<T>();
			this.m_result = DwgEntitiesSelectionResult.eOK;
			try
			{
				DwgEntitiesSelectFilter dwgEntitiesSelectFilter = new DwgEntitiesSelectFilter(this.m_doc, SelecteEntType.eGeoIns, null);
				Reference reference = this.m_uiDocument.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)2, dwgEntitiesSelectFilter, strPickGuidInfo);
				Element element = this.m_doc.GetElement(reference);
				GeometryElement geometryElement = element.get_Geometry(new Options());
				GeometryObject geometryObjectFromReference = element.GetGeometryObjectFromReference(reference);
				bool flag = !this.PreCheck(element);
				if (flag)
				{
					return false;
				}
				PickedBox pickedBox = null;
				if (bPointBox)
				{
					pickedBox = this.m_uiDocument.Selection.PickBox((Autodesk.Revit.UI.Selection.PickBoxStyle)1, strPickBoxGuidInfo);
				}
				string strLayerName = string.Empty;
				Category category = null;
				ElementId elementId = null;
				bool flag2 = !this.GetGraphicsInfoFromGeometryObject(out elementId, out category, geometryObjectFromReference, false);
				if (flag2)
				{
					this.m_result = DwgEntitiesSelectionResult.eDataError;
					return false;
				}
				bool flag3 = category == null;
				if (flag3)
				{
					this.m_result = DwgEntitiesSelectionResult.eDataError;
					return false;
				}
				strLayerName = category.Name;
				string empty = string.Empty;
				bool flag4 = !this.GetCADLinkPath(element, out empty);
				if (flag4)
				{
					this.m_result = DwgEntitiesSelectionResult.eDwgPathError;
					return false;
				}
				bool flag5 = !File.Exists(empty);
				if (flag5)
				{
					this.m_result = DwgEntitiesSelectionResult.eDwgPathError;
					return false;
				}
				DwgManager dwgManager = null;
				try
				{
					dwgManager = new DwgManager(empty);
				}
				catch (Exception)
				{
					this.m_result = DwgEntitiesSelectionResult.eNotAnalysisDwg;
					return false;
				}
				OdDbBlockReference odDbBlockReference = null;
				XYZ xyz = new XYZ(reference.GlobalPoint.X, reference.GlobalPoint.Y, 0.0);
				Transform inverse = (element as ImportInstance).GetTotalTransform().Inverse;
				XYZ xyz2 = inverse.OfPoint(xyz) * 304.8;
				bool flag6 = !dwgManager.TryGetBlockRefByPos(strLayerName, new OdGePoint3d(xyz2.X, xyz2.Y, 0.0), ref odDbBlockReference);
				if (flag6)
				{
					this.m_result = DwgEntitiesSelectionResult.eNotGetBlock;
					return false;
				}
				string blockName = dwgManager.GetBlockName(odDbBlockReference);
				List<OdDbBlockReference> list = new List<OdDbBlockReference>();
				OdGeScale3d odGeScale3d = null;
				if (bOnlySameSize)
				{
					odGeScale3d = new OdGeScale3d();
					odGeScale3d.sx = odDbBlockReference.scaleFactors().sx;
					odGeScale3d.sy = odDbBlockReference.scaleFactors().sy;
				}
				dwgManager.TryGetBlockRefByName(blockName, ref list, odGeScale3d);
				List<OdDbBlockReference> list2 = new List<OdDbBlockReference>();
				bool flag7 = pickedBox != null;
				if (flag7)
				{
					XYZ ptMin = new XYZ(Math.Min(pickedBox.Min.X, pickedBox.Max.X), Math.Min(pickedBox.Min.Y, pickedBox.Max.Y), 0.0);
					XYZ ptMax = new XYZ(Math.Max(pickedBox.Min.X, pickedBox.Max.X), Math.Max(pickedBox.Min.Y, pickedBox.Max.Y), 0.0);
					list2 = dwgManager.GetBlockRefInRange(list, ptMin, ptMax, (element as ImportInstance).GetTotalTransform());
				}
				else
				{
					list2.AddRange(list);
				}
				foreach (OdDbBlockReference odDbBlockReference2 in list)
				{
					T t = (T)((object)Activator.CreateInstance(typeof(T), new object[]
					{
						odDbBlockReference2,
						element as ImportInstance
					}));
					t.RevitLayer = category;
					lstBlockInfo.Add(t);
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				this.m_result = DwgEntitiesSelectionResult.eCancel;
				throw;
			}
			catch (Exception ex)
			{
				Trace.Assert(true, ex.Message);
				this.m_result = DwgEntitiesSelectionResult.eOtherError;
				return false;
			}
			return true;
		}

		public virtual bool TryPickOneBlockRef(out BlockInfo blockInfo, string strPickGuidInfo = "")
		{
			blockInfo = null;
			this.m_result = DwgEntitiesSelectionResult.eOK;
			try
			{
				DwgEntitiesSelectFilter dwgEntitiesSelectFilter = new DwgEntitiesSelectFilter(this.m_doc, SelecteEntType.eGeoIns, null);
				Reference reference = this.m_uiDocument.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)2, dwgEntitiesSelectFilter, strPickGuidInfo);
				Element element = this.m_doc.GetElement(reference);
				GeometryElement geometryElement = element.get_Geometry(new Options());
				GeometryObject geometryObjectFromReference = element.GetGeometryObjectFromReference(reference);
				bool flag = !this.PreCheck(element);
				if (flag)
				{
					return false;
				}
				string strLayerName = string.Empty;
				Category category = null;
				ElementId elementId = null;
				bool flag2 = !this.GetGraphicsInfoFromGeometryObject(out elementId, out category, geometryObjectFromReference, false);
				if (flag2)
				{
					this.m_result = DwgEntitiesSelectionResult.eDataError;
					return false;
				}
				bool flag3 = category == null;
				if (flag3)
				{
					this.m_result = DwgEntitiesSelectionResult.eDataError;
					return false;
				}
				strLayerName = category.Name;
				string empty = string.Empty;
				bool flag4 = !this.GetCADLinkPath(element, out empty);
				if (flag4)
				{
					this.m_result = DwgEntitiesSelectionResult.eDwgPathError;
					return false;
				}
				bool flag5 = !File.Exists(empty);
				if (flag5)
				{
					this.m_result = DwgEntitiesSelectionResult.eDwgPathError;
					return false;
				}
				DwgManager dwgManager = null;
				try
				{
					dwgManager = new DwgManager(empty);
				}
				catch (Exception)
				{
					this.m_result = DwgEntitiesSelectionResult.eNotAnalysisDwg;
					return false;
				}
				OdDbBlockReference odDbBlockRef = null;
				XYZ xyz = reference.GlobalPoint * 304.8;
				bool flag6 = !dwgManager.TryGetBlockRefByPos(strLayerName, new OdGePoint3d(xyz.X, xyz.Y, 0.0), ref odDbBlockRef);
				if (flag6)
				{
					this.m_result = DwgEntitiesSelectionResult.eNotGetBlock;
					return false;
				}
				blockInfo = new BlockInfo(odDbBlockRef, element as ImportInstance);
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				this.m_result = DwgEntitiesSelectionResult.eCancel;
				throw;
			}
			catch (Exception ex)
			{
				Trace.Assert(true, ex.Message);
				this.m_result = DwgEntitiesSelectionResult.eOtherError;
				return false;
			}
			return true;
		}

		public virtual bool TryPickSameLayerBlockRef<T>(out List<T> lstBlockInfo, bool bHideLayer = false, string strPickGuidInfo = "") where T : BlockInfo
		{
			lstBlockInfo = new List<T>();
			this.m_result = DwgEntitiesSelectionResult.eOK;
			try
			{
				DwgEntitiesSelectFilter dwgEntitiesSelectFilter = new DwgEntitiesSelectFilter(this.m_doc, SelecteEntType.eGeoIns, null);
				Reference reference = this.m_uiDocument.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)2, dwgEntitiesSelectFilter, strPickGuidInfo);
				Element element = this.m_doc.GetElement(reference);
				GeometryElement geometryElement = element.get_Geometry(new Options());
				GeometryObject geometryObjectFromReference = element.GetGeometryObjectFromReference(reference);
				bool flag = !this.PreCheck(element);
				if (flag)
				{
					return false;
				}
				string strLayerName = string.Empty;
				Category category = null;
				ElementId elementId = null;
				bool flag2 = !this.GetGraphicsInfoFromGeometryObject(out elementId, out category, geometryObjectFromReference, bHideLayer);
				if (flag2)
				{
					this.m_result = DwgEntitiesSelectionResult.eDataError;
					return false;
				}
				bool flag3 = category == null;
				if (flag3)
				{
					this.m_result = DwgEntitiesSelectionResult.eDataError;
					return false;
				}
				strLayerName = category.Name;
				string empty = string.Empty;
				bool flag4 = !this.GetCADLinkPath(element, out empty);
				if (flag4)
				{
					this.m_result = DwgEntitiesSelectionResult.eDwgPathError;
					return false;
				}
				bool flag5 = !File.Exists(empty);
				if (flag5)
				{
					this.m_result = DwgEntitiesSelectionResult.eDwgPathError;
					return false;
				}
				DwgManager dwgManager = null;
				try
				{
					dwgManager = new DwgManager(empty);
				}
				catch (Exception)
				{
					this.m_result = DwgEntitiesSelectionResult.eNotAnalysisDwg;
					return false;
				}
				List<OdDbBlockReference> list = new List<OdDbBlockReference>();
				dwgManager.TryGetBlockRefByLayer(strLayerName, ref list);
				List<OdDbBlockReference> list2 = new List<OdDbBlockReference>();
				foreach (OdDbBlockReference odDbBlockReference in list)
				{
					T t = (T)((object)Activator.CreateInstance(typeof(T), new object[]
					{
						odDbBlockReference,
						element as ImportInstance
					}));
					t.RevitLayer = category;
					lstBlockInfo.Add(t);
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				this.m_result = DwgEntitiesSelectionResult.eCancel;
				throw;
			}
			catch (Exception ex)
			{
				Trace.Assert(true, ex.Message);
				this.m_result = DwgEntitiesSelectionResult.eOtherError;
				return false;
			}
			return true;
		}

		public virtual bool TryPickSameLayerText(out List<TextInfo> lstText, bool bHideLayer = false, string strPickGuidInfo = "")
		{
			lstText = new List<TextInfo>();
			this.m_result = DwgEntitiesSelectionResult.eOK;
			try
			{
				DwgEntitiesSelectFilter dwgEntitiesSelectFilter = new DwgEntitiesSelectFilter(this.m_doc, SelecteEntType.eText, null);
				Reference reference = this.m_uiDocument.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)2, dwgEntitiesSelectFilter, strPickGuidInfo);
				Element element = this.m_doc.GetElement(reference);
				GeometryElement geometryElement = element.get_Geometry(new Options());
				GeometryObject geometryObjectFromReference = element.GetGeometryObjectFromReference(reference);
				bool flag = !this.PreCheck(element);
				if (flag)
				{
					return false;
				}
				string strLayerName = string.Empty;
				Category category = null;
				ElementId elementId = null;
				bool flag2 = !this.GetGraphicsInfoFromGeometryObject(out elementId, out category, geometryObjectFromReference, bHideLayer);
				if (flag2)
				{
					this.m_result = DwgEntitiesSelectionResult.eDataError;
					return false;
				}
				bool flag3 = category == null;
				if (flag3)
				{
					this.m_result = DwgEntitiesSelectionResult.eDataError;
					return false;
				}
				strLayerName = category.Name;
				string empty = string.Empty;
				bool flag4 = !this.GetCADLinkPath(element, out empty);
				if (flag4)
				{
					this.m_result = DwgEntitiesSelectionResult.eDwgPathError;
					return false;
				}
				bool flag5 = !File.Exists(empty);
				if (flag5)
				{
					this.m_result = DwgEntitiesSelectionResult.eDwgPathError;
					return false;
				}
				DwgManager dwgManager = null;
				try
				{
					dwgManager = new DwgManager(empty);
				}
				catch (Exception)
				{
					this.m_result = DwgEntitiesSelectionResult.eNotAnalysisDwg;
					return false;
				}
				List<DwgTextInfo> list = new List<DwgTextInfo>();
				bool flag6 = !dwgManager.TryGetTextByLayer(strLayerName, ref list);
				if (flag6)
				{
					this.m_result = DwgEntitiesSelectionResult.eOtherError;
					return false;
				}
				bool flag7 = !dwgManager.TryGetMTextByLayer(strLayerName, ref list);
				if (flag7)
				{
					this.m_result = DwgEntitiesSelectionResult.eOtherError;
					return false;
				}
				bool flag8 = !dwgManager.TryGetAttribueTextByLayer(strLayerName, ref list);
				if (flag8)
				{
					this.m_result = DwgEntitiesSelectionResult.eOtherError;
					return false;
				}
				foreach (DwgTextInfo dwgTextInfo in list)
				{
					bool flag9 = dwgTextInfo.ItemName.IsEmpty<char>();
					if (!flag9)
					{
						TextInfo textInfo = new TextInfo(dwgTextInfo, strLayerName, element as ImportInstance);
						textInfo.RevitLayer = category;
						lstText.Add(textInfo);
					}
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				this.m_result = DwgEntitiesSelectionResult.eCancel;
				throw;
			}
			catch (Exception ex)
			{
				Trace.Assert(true, ex.Message);
				this.m_result = DwgEntitiesSelectionResult.eOtherError;
				return false;
			}
			return true;
		}

		public virtual bool TryPickAxisNumber(out List<ICadElement> lstElement, bool bHideLayer = false, string strPickGuidInfo = "")
		{
			lstElement = new List<ICadElement>();
			this.m_result = DwgEntitiesSelectionResult.eOK;
			try
			{
				SelecteEntType selecteEntType = SelecteEntType.eLine | SelecteEntType.eArc | SelecteEntType.ePolyLine;
				DwgEntitiesSelectFilter dwgEntitiesSelectFilter = new DwgEntitiesSelectFilter(this.m_doc, selecteEntType | SelecteEntType.eText, null);
				Reference reference = this.m_uiDocument.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)2, dwgEntitiesSelectFilter, strPickGuidInfo);
				Element element = this.m_doc.GetElement(reference);
				GeometryElement geometryElement = element.get_Geometry(new Options());
				GeometryObject geometryObjectFromReference = element.GetGeometryObjectFromReference(reference);
				bool flag = !this.PreCheck(element);
				if (flag)
				{
					return false;
				}
				string text = string.Empty;
				Category category = null;
				ElementId elementId = null;
				bool flag2 = !this.GetGraphicsInfoFromGeometryObject(out elementId, out category, geometryObjectFromReference, bHideLayer);
				if (flag2)
				{
					this.m_result = DwgEntitiesSelectionResult.eDataError;
					return false;
				}
				bool flag3 = category == null;
				if (flag3)
				{
					this.m_result = DwgEntitiesSelectionResult.eDataError;
					return false;
				}
				text = category.Name;
				bool flag4 = geometryObjectFromReference.GetType().ToString() == "Autodesk.Revit.DB.GeometryObject";
				if (flag4)
				{
					string empty = string.Empty;
					bool flag5 = !this.GetCADLinkPath(element, out empty);
					if (flag5)
					{
						this.m_result = DwgEntitiesSelectionResult.eDwgPathError;
						return false;
					}
					bool flag6 = !File.Exists(empty);
					if (flag6)
					{
						this.m_result = DwgEntitiesSelectionResult.eDwgPathError;
						return false;
					}
					DwgManager dwgManager = null;
					try
					{
						dwgManager = new DwgManager(empty);
					}
					catch (Exception)
					{
						this.m_result = DwgEntitiesSelectionResult.eNotAnalysisDwg;
						return false;
					}
					List<DwgTextInfo> list = new List<DwgTextInfo>();
					bool flag7 = !dwgManager.TryGetTextByLayer(text, ref list);
					if (flag7)
					{
						this.m_result = DwgEntitiesSelectionResult.eOtherError;
						return false;
					}
					bool flag8 = !dwgManager.TryGetMTextByLayer(text, ref list);
					if (flag8)
					{
						this.m_result = DwgEntitiesSelectionResult.eOtherError;
						return false;
					}
					bool flag9 = !dwgManager.TryGetAttribueTextByLayer(text, ref list);
					if (flag9)
					{
						this.m_result = DwgEntitiesSelectionResult.eOtherError;
						return false;
					}
					foreach (DwgTextInfo dwgTextInfo in list)
					{
						bool flag10 = dwgTextInfo.ItemName.IsEmpty<char>();
						if (!flag10)
						{
							TextInfo textInfo = new TextInfo(dwgTextInfo, text, element as ImportInstance);
							textInfo.RevitLayer = category;
							lstElement.Add(textInfo);
						}
					}
				}
				else
				{
					foreach (GeometryObject geometryObject in geometryElement)
					{
						GeometryInstance geometryInstance = geometryObject as GeometryInstance;
						bool flag11 = null != geometryInstance;
						if (flag11)
						{
							foreach (GeometryObject geometryObject2 in geometryInstance.GetInstanceGeometry())
							{
								bool flag12 = geometryObject2.GraphicsStyleId.IntegerValue != elementId.IntegerValue;
								if (!flag12)
								{
									bool flag13 = !DwgEntitiesSelectFilter.IsSpecifiedType(geometryObject2, selecteEntType);
									if (!flag13)
									{
										bool flag14 = geometryObject2 is Curve;
										if (flag14)
										{
											Curve curve = geometryObject2 as Curve;
											CurveInfo curveInfo = new CurveInfo(curve, text);
											curveInfo.RevitLayer = category;
											lstElement.Add(curveInfo);
										}
										else
										{
											bool flag15 = geometryObject2 is PolyLine;
											if (flag15)
											{
												PolyLine polyLine = geometryObject2 as PolyLine;
												IList<XYZ> coordinates = polyLine.GetCoordinates();
												for (int i = 0; i < coordinates.Count - 1; i++)
												{
													Line curve2 = Line.CreateBound(coordinates[i], coordinates[i + 1]);
													CurveInfo curveInfo2 = new CurveInfo(curve2, text);
													curveInfo2.RevitLayer = category;
													lstElement.Add(curveInfo2);
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				this.m_result = DwgEntitiesSelectionResult.eCancel;
				throw;
			}
			catch (Exception ex)
			{
				Trace.Assert(true, ex.Message);
				this.m_result = DwgEntitiesSelectionResult.eOtherError;
				return false;
			}
			return true;
		}

		public virtual bool TryGetEntitesInPickBox<T>(out List<T> lstEntityInRange, List<T> lstAllEntity, DelegateInRange<T> delegateInRange, string strPickBoxGuidInfo = "")
		{
			lstEntityInRange = new List<T>();
			this.m_result = DwgEntitiesSelectionResult.eOK;
			try
			{
				PickedBox box = this.m_uiDocument.Selection.PickBox((Autodesk.Revit.UI.Selection.PickBoxStyle)1, strPickBoxGuidInfo);
				foreach (T t in lstAllEntity)
				{
					bool flag = delegateInRange(t, box);
					if (flag)
					{
						lstEntityInRange.Add(t);
					}
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				this.m_result = DwgEntitiesSelectionResult.eCancel;
				throw;
			}
			catch (Exception ex)
			{
				Trace.Assert(true, ex.Message);
				this.m_result = DwgEntitiesSelectionResult.eOtherError;
				return false;
			}
			return true;
		}

		protected bool GetCADLinkPath(Element eleCadLink, out string strDwgPath)
		{
			strDwgPath = string.Empty;
			try
			{
				ImportInstance importInstance = eleCadLink as ImportInstance;
				bool flag = importInstance == null;
				if (flag)
				{
					return false;
				}
				CADLinkType cadlinkType = this.m_doc.GetElement(importInstance.GetTypeId()) as CADLinkType;
				bool flag2 = cadlinkType == null;
				if (flag2)
				{
					return false;
				}
				ExternalFileReference externalFileReference = cadlinkType.GetExternalFileReference();
				ModelPath absolutePath = externalFileReference.GetAbsolutePath();
				strDwgPath = ModelPathUtils.ConvertModelPathToUserVisiblePath(absolutePath);
			}
			catch (Exception ex)
			{
				Trace.Assert(true, ex.Message);
				return false;
			}
			return true;
		}

		protected bool GetGraphicsInfoFromGeometryObject(out ElementId graphicsStyleId, out Category graphicsStyleCategory, GeometryObject geoObj, bool bHideLayer)
		{
			graphicsStyleId = null;
			graphicsStyleCategory = null;
			try
			{
				bool flag = geoObj.GraphicsStyleId == ElementId.InvalidElementId;
				if (flag)
				{
					return false;
				}
				graphicsStyleId = geoObj.GraphicsStyleId;
				GraphicsStyle graphicsStyle = this.m_doc.GetElement(geoObj.GraphicsStyleId) as GraphicsStyle;
				bool flag2 = graphicsStyle != null;
				if (flag2)
				{
					graphicsStyleCategory = graphicsStyle.GraphicsStyleCategory;
				}
				if (bHideLayer)
				{
					bool flag3 = graphicsStyleCategory != null;
					if (flag3)
					{
						this.m_doc.ActiveView.HYSetVisibility(graphicsStyleCategory, false);
					}
				}
			}
			catch (Exception)
			{
				return false;
			}
			return true;
		}

		protected bool CheckImportInsUnit(Element eleCadLink)
		{
			try
			{
				ImportInstance importInstance = eleCadLink as ImportInstance;
				bool flag = importInstance == null;
				if (flag)
				{
					return false;
				}
				CADLinkType cadlinkType = this.m_doc.GetElement(importInstance.GetTypeId()) as CADLinkType;
				bool flag2 = cadlinkType == null;
				if (flag2)
				{
					return false;
				}
				Parameter parameter = cadlinkType.GetParameter(BuiltInParameter.IMPORT_DISPLAY_UNITS);
				bool flag3 = parameter.AsInteger() != 2;
				if (flag3)
				{
					return false;
				}
			}
			catch (Exception ex)
			{
				Trace.Assert(true, ex.Message);
				return false;
			}
			return true;
		}

		protected bool CheckLinkDwg(Element eleCadLink)
		{
			try
			{
				ImportInstance importInstance = eleCadLink as ImportInstance;
				bool flag = importInstance == null;
				if (flag)
				{
					return false;
				}
				CADLinkType cadlinkType = this.m_doc.GetElement(importInstance.GetTypeId()) as CADLinkType;
				bool flag2 = cadlinkType == null;
				if (flag2)
				{
					return false;
				}
				bool flag3 = !cadlinkType.IsExternalFileReference();
				if (flag3)
				{
					return false;
				}
				ExternalFileReference externalFileReference = cadlinkType.GetExternalFileReference();
				ModelPath absolutePath = externalFileReference.GetAbsolutePath();
				string text = ModelPathUtils.ConvertModelPathToUserVisiblePath(absolutePath);
				bool flag4 = !text.EndsWith(".dwg", true, null);
				if (flag4)
				{
					return false;
				}
			}
			catch (Exception ex)
			{
				Trace.Assert(true, ex.Message);
				return false;
			}
			return true;
		}

		protected bool PreCheck(Element elem)
		{
			bool flag = !this.CheckLinkDwg(elem);
			bool result;
			if (flag)
			{
				this.m_result = DwgEntitiesSelectionResult.eNotLinkDwg;
				result = false;
			}
			else
			{
				string empty = string.Empty;
				bool flag2 = !this.GetCADLinkPath(elem, out empty);
				if (flag2)
				{
					this.m_result = DwgEntitiesSelectionResult.eDwgPathError;
					result = false;
				}
				else
				{
					bool flag3 = !File.Exists(empty);
					if (flag3)
					{
						this.m_result = DwgEntitiesSelectionResult.eDwgPathError;
						result = false;
					}
					else
					{
						bool flag4 = !this.CheckImportInsUnit(elem);
						if (flag4)
						{
							this.m_result = DwgEntitiesSelectionResult.eUnitError;
							result = false;
						}
						else
						{
							result = true;
						}
					}
				}
			}
			return result;
		}

		private UIDocument m_uiDocument = null;

		private Document m_doc = null;

		private DwgEntitiesSelectionResult m_result = DwgEntitiesSelectionResult.eOK;
	}
}
